﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantAreaDao : IRestaurantAreaDao
    {
        public List<RestaurantArea> GetRestaurantAreas()
        {
            string procedures = "Get_All_RestaurantAreas";

            return Db.ReadList(procedures, true, Make);
        }
        public List<RestaurantArea> GetRestaurantAreasActiveByBranch(string restaurantId)
        {
            string procedures = "Get_RestaurantAreas_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "' AND IsActive = 1" };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public List<RestaurantArea> GetRestaurantAreasByBranch(string restaurantId)
        {
            string procedures = "Get_RestaurantAreas_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public List<RestaurantArea> GetRestaurantAreaByPageAndBranch(int currentPage, string restaurantdId)
        {
            string procedures = "Get_Paged_RestaurantAreas";

            object[] parms = {  "@WhereClause", " RestaurantID = '" + restaurantdId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }
        public RestaurantArea GetRestaurantArea(string restaurantAreaId)
        {
            string procedures = "Get_All_RestaurantArea_ByID";

            object[] parms = { "@RestaurantAreaID", restaurantAreaId };
            return Db.Read(procedures, true, Make, parms);
        }
        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_RestaurantArea";

            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }
        public int GetTotalPageByBranch(string restaurantId)
        {
            string procedures = "Get_PageSize_RestaurantArea";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };
            return Db.ReadTotalPage(procedures, true, parms);
        }
        public void InsertRestaurantArea(RestaurantArea restaurantArea)
        {
            string sql = "Insert_RestaurantArea";

            restaurantArea.RestaurantAreaID = Db.Insert(sql, true, Take(restaurantArea));
        }
        public void UpdateRestaurantArea(RestaurantArea restaurantArea)
        {
            string sql = "Update_RestaurantArea";

            Db.Update(sql, true, Take(restaurantArea));
        }
        public void DeleteRestaurantArea(RestaurantArea restaurantArea)
        {
            string sql = "Delete_RestaurantArea";

            Db.Update(sql, true, Take(restaurantArea.RestaurantAreaID));
        }
        private static Func<IDataReader, RestaurantArea> Make = reader =>
            new RestaurantArea
            {
                RestaurantAreaID = reader["RestaurantAreaID"].AsString(),
                RestaurantAreaCode = reader["RestaurantAreaCode"].AsString(),
                RestaurantAreaName = reader["RestaurantAreaName"].AsString(),
                Description = reader["Description"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
        private object[] Take(RestaurantArea restaurantArea)
        {
            return new object[]  
            {
                "@RestaurantAreaID", restaurantArea.RestaurantAreaID,
                "@RestaurantAreaCode", restaurantArea.RestaurantAreaCode,
                "@RestaurantAreaName", restaurantArea.RestaurantAreaName,
                "@RestaurantID", restaurantArea.RestaurantID,
                "@Description", restaurantArea.Description,
                "@IsActive", restaurantArea.IsActive
            };
        }
        private object[] Take(string restaurantAreaId)
        {
            return new object[]  
            {
                "@RestaurantAreaID", restaurantAreaId,
            };
        }
    }
}
